package cn.iocoder.yudao.module.call.service.ivrrobotrole;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.call.controller.admin.ivrrobotrole.vo.IvrRobotRolePageReqVO;
import cn.iocoder.yudao.module.call.controller.admin.ivrrobotrole.vo.IvrRobotRoleSaveReqVO;
import cn.iocoder.yudao.module.call.dal.dataobject.ivrrobotrole.IvrRobotRoleDO;
import cn.iocoder.yudao.module.call.dal.mysql.ivrrobotrole.IvrRobotRoleMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.call.enums.ErrorCodeConstants.ROBOT_ROLE_NOT_EXISTS;
/**
 * ivr大模型角色 Service 实现类
 *
 * @author 数创万维
 */
@Service
@Validated
public class IvrRobotRoleServiceImpl implements IvrRobotRoleService {

    @Resource
    private IvrRobotRoleMapper robotRoleMapper;

    @Override
    public Long createRobotRole(IvrRobotRoleSaveReqVO createReqVO) {
        // 插入
        IvrRobotRoleDO robotRole = BeanUtils.toBean(createReqVO, IvrRobotRoleDO.class);
        robotRoleMapper.insert(robotRole);
        // 返回
        return robotRole.getId();
    }

    @Override
    public Long createRobotRoleAndUpData(IvrRobotRoleSaveReqVO createReqVO) {
        IvrRobotRoleDO robotRole = BeanUtils.toBean(createReqVO, IvrRobotRoleDO.class);
        robotRoleMapper.insertOrUpdate(robotRole);
        // 返回
        return robotRole.getId();
    }

    @Override
    public void updateRobotRole(IvrRobotRoleSaveReqVO updateReqVO) {
        // 校验存在
        validateRobotRoleExists(updateReqVO.getId());
        // 更新
        IvrRobotRoleDO updateObj = BeanUtils.toBean(updateReqVO, IvrRobotRoleDO.class);
        robotRoleMapper.updateById(updateObj);
    }

    @Override
    public void deleteRobotRole(Long id) {
        // 校验存在
        validateRobotRoleExists(id);
        // 删除
        robotRoleMapper.deleteById(id);
    }

    private void validateRobotRoleExists(Long id) {
        if (robotRoleMapper.selectById(id) == null) {
            throw exception(ROBOT_ROLE_NOT_EXISTS);
        }
    }

    @Override
    public IvrRobotRoleDO getRobotRole(Long id) {
        return robotRoleMapper.selectById(id);
    }
    @Override
    public IvrRobotRoleDO getRobotRoleByFlowId(Long flowId) {
        LambdaQueryWrapper<IvrRobotRoleDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(IvrRobotRoleDO::getIvrFlowId, flowId);
        queryWrapper.last("limit 1");
        IvrRobotRoleDO ivrRobotRoleDO = robotRoleMapper.selectOne(queryWrapper);
        return ivrRobotRoleDO;
    }

    @Override
    public PageResult<IvrRobotRoleDO> getRobotRolePage(IvrRobotRolePageReqVO pageReqVO) {
        return robotRoleMapper.selectPage(pageReqVO);
    }

}